/*
 * Copyright 2013 Agustina Martinez-Garcia

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package uk.ljmu.fedoraifz.wadl.fedora34;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.List;

import javax.activation.DataSource;
import javax.xml.bind.JAXBContext;
//import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;

import org.apache.commons.mail.ByteArrayDataSource;
import org.apache.log4j.Logger;
import org.igfay.jfig.JFig;
import org.igfay.jfig.JFigException;
import org.jvnet.ws.wadl.util.DSDispatcher;
//import org.jvnet.ws.wadl.util.JAXBDispatcher;
import org.jvnet.ws.wadl.util.UriBuilder;

import uk.ljmu.fedoraifz.restrequests.UserCredentials;

public class PidRelationships {
    private static Logger log = Logger.getLogger(PidRelationships.class);
    //private JAXBDispatcher _jaxbDispatcher;
    private DSDispatcher _dsDispatcher;
    private UriBuilder _uriBuilder;
    @SuppressWarnings("unused")
    private JAXBContext _jc;
    private HashMap<String, Object> _templateAndMatrixParameterValues;
    private List<String> matrixParamSet;
    private DataSource retVal;

    public void setMatrixParamSet(List<String> matrixParamSet) {
	this.matrixParamSet = matrixParamSet;
    }

    public List<String> getMatrixParamSet() {
	return matrixParamSet;
    }

    public void setRetVal(DataSource retVal) {
	this.retVal = retVal;
    }

    public DataSource getRetVal() {
	return retVal;
    }

    /**
     * Create new instance
     * 
     */
    public PidRelationships(String pid)
    throws JAXBException
    {
	_dsDispatcher = new DSDispatcher();
	_uriBuilder = new UriBuilder();
	//Extract bassePath from config file
	String basePath;
	try {
	    basePath = JFig.getInstance().getValue("fedoraServer", "restService");
	} catch (JFigException e) {
	    log.error("Error: ", e);
	    //Error, use default Fedora path
	    basePath = "http://localhost:8080/fedora";
	}
	matrixParamSet = _uriBuilder.addPathSegment(basePath);
	matrixParamSet = _uriBuilder.addPathSegment("/objects");
	matrixParamSet = _uriBuilder.addPathSegment("/{pid}/relationships");
	_templateAndMatrixParameterValues = new HashMap<String, Object>();
	_templateAndMatrixParameterValues.put("pid", pid);
    }

    /**
     * Get pid
     * 
     */
    public String getPid() {
	return ((String) _templateAndMatrixParameterValues.get("pid"));
    }

    /**
     * Set pid
     * 
     */
    public void setPid(String pid) {
	_templateAndMatrixParameterValues.put("pid", pid);
    }

    public void get(UserCredentials userCreds)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	//AMG Authorization
	_headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    public void get()
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    /*public void getAsvoid()
    throws IOException, MalformedURLException, JAXBException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	if (_retVal == null) {
	    return ;
	}
	if (JAXBElement.class.isInstance(_retVal)) {
	    JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
	    _retVal = jaxbElement.getValue();
	}
	return ;
    }*/

    public void get(UserCredentials userCreds, String subject, String predicate, String format)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	//AMG - Authorization
	_headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());

	if (subject != null && !subject.equals(""))
	    _queryParameterValues.put("subject", subject);
	if (predicate != null && !predicate.equals(""))
	    _queryParameterValues.put("predicate", predicate);
	if (format != null && !format.equals(""))
	    _queryParameterValues.put("format", format);

	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    public void get(String subject, String predicate, String format)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();

	if (subject != null && !subject.equals(""))
	    _queryParameterValues.put("subject", subject);
	if (predicate != null && !predicate.equals(""))
	    _queryParameterValues.put("predicate", predicate);
	if (format != null && !format.equals(""))
	    _queryParameterValues.put("format", format);

	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doGET(_url, _headerParameterValues, null);
	return ;
    }

    /*public void getAsvoid(String subject, String predicate, String format)
    throws IOException, MalformedURLException, JAXBException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	_queryParameterValues.put("subject", subject);
	_queryParameterValues.put("predicate", predicate);
	_queryParameterValues.put("format", format);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	Object _retVal = _jaxbDispatcher.doGET(_url, _headerParameterValues, null);
	if (_retVal == null) {
	    return ;
	}
	if (JAXBElement.class.isInstance(_retVal)) {
	    JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
	    _retVal = jaxbElement.getValue();
	}
	return ;
    }*/

    public void delete(UserCredentials userCreds)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	//AMG - Authorization
	_headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doDELETE(_url, _headerParameterValues, null);
	return ;
    }

    public void delete()
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doDELETE(_url, _headerParameterValues, null);
	return ;
    }

    /* public void deleteAsvoid()
    throws IOException, MalformedURLException, JAXBException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	Object _retVal = _jaxbDispatcher.doDELETE(_url, _headerParameterValues, null);
	if (_retVal == null) {
	    return ;
	}
	if (JAXBElement.class.isInstance(_retVal)) {
	    JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
	    _retVal = jaxbElement.getValue();
	}
	return ;
    }*/

    public void delete(UserCredentials userCreds, String subject, String predicate, String object, Boolean isliteral, String datatype)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	//AMG - Authorization
	_headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());

	if (subject != null && !subject.equals(""))
	    _queryParameterValues.put("subject", subject);
	if (predicate != null && !predicate.equals(""))
	    _queryParameterValues.put("predicate", predicate);
	if (object != null && !object.equals(""))
	    _queryParameterValues.put("object", object);
	if (isliteral != null)
	    _queryParameterValues.put("isLiteral", isliteral);
	if (datatype != null && !datatype.equals(""))
	    _queryParameterValues.put("datatype", datatype);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doDELETE(_url, _headerParameterValues, null);
	return ;
    }

    public void delete(String subject, String predicate, String object, Boolean isliteral, String datatype)
    throws IOException, MalformedURLException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	if (subject != null && !subject.equals(""))
	    _queryParameterValues.put("subject", subject);
	if (predicate != null && !predicate.equals(""))
	    _queryParameterValues.put("predicate", predicate);
	if (object != null && !object.equals(""))
	    _queryParameterValues.put("object", object);
	if (isliteral != null)
	    _queryParameterValues.put("isLiteral", isliteral);
	if (datatype != null && !datatype.equals(""))
	    _queryParameterValues.put("datatype", datatype);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	retVal = _dsDispatcher.doDELETE(_url, _headerParameterValues, null);
	return ;
    }

    /* public void deleteAsvoid(String subject, String predicate, String object, Boolean isliteral, String datatype)
    throws IOException, MalformedURLException, JAXBException
    {
	HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	_queryParameterValues.put("subject", subject);
	_queryParameterValues.put("predicate", predicate);
	_queryParameterValues.put("object", object);
	_queryParameterValues.put("isLiteral", isliteral);
	_queryParameterValues.put("datatype", datatype);
	String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	Object _retVal = _jaxbDispatcher.doDELETE(_url, _headerParameterValues, null);
	if (_retVal == null) {
	    return ;
	}
	if (JAXBElement.class.isInstance(_retVal)) {
	    JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
	    _retVal = jaxbElement.getValue();
	}
	return ;
    }
     */
    public static class New {

	private static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
	//private JAXBDispatcher _jaxbDispatcher;
	private DSDispatcher _dsDispatcher;
	private UriBuilder _uriBuilder;
	@SuppressWarnings("unused")
	private JAXBContext _jc;
	private HashMap<String, Object> _templateAndMatrixParameterValues;
	private List<String> matrixParamSet;
	private DataSource retVal;

	public void setMatrixParamSet(List<String> matrixParamSet) {
	    this.matrixParamSet = matrixParamSet;
	}

	public List<String> getMatrixParamSet() {
	    return matrixParamSet;
	}

	public void setRetVal(DataSource retVal) {
	    this.retVal = retVal;
	}

	public DataSource getRetVal() {
	    return retVal;
	}

	/**
	 * Create new instance
	 * 
	 */
	public New(String pid)
	throws JAXBException
	{
	    _dsDispatcher = new DSDispatcher();
	    _uriBuilder = new UriBuilder();

	    //Extract bassePath from config file
	    String basePath;
	    try {
		basePath = JFig.getInstance().getValue("fedoraServer", "restService");
	    } catch (JFigException e) {
		log.error("Error: ", e);
		//Error, use default Fedora path
		basePath = "http://localhost:8080/fedora";
	    }

	    matrixParamSet = _uriBuilder.addPathSegment(basePath);
	    matrixParamSet = _uriBuilder.addPathSegment("/objects");
	    matrixParamSet = _uriBuilder.addPathSegment("/{pid}/relationships");
	    matrixParamSet = _uriBuilder.addPathSegment("/new");
	    _templateAndMatrixParameterValues = new HashMap<String, Object>();
	    _templateAndMatrixParameterValues.put("pid", pid);
	}

	/**
	 * Get pid
	 * 
	 */
	public String getPid() {
	    return ((String) _templateAndMatrixParameterValues.get("pid"));
	}

	/**
	 * Set pid
	 * 
	 */
	public void setPid(String pid) {
	    _templateAndMatrixParameterValues.put("pid", pid);
	}

	public void post(UserCredentials userCreds)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Authorization
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doPOST(new ByteArrayDataSource("", DEFAULT_CONTENT_TYPE), DEFAULT_CONTENT_TYPE, _url, _headerParameterValues, null);
	    return ;
	}

	public void post()
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doPOST(new ByteArrayDataSource("", DEFAULT_CONTENT_TYPE), DEFAULT_CONTENT_TYPE, _url, _headerParameterValues, null);
	    return ;
	}

	/*public void postAsvoid()
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doPOST(null, null, _url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}*/

	public void post(UserCredentials userCreds, String subject, String predicate, String object, Boolean isliteral, String datatype)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    //AMG - Authorization
	    _headerParameterValues.put("Authorization", userCreds.getEncodedCredentials());

	    if (subject != null && !subject.equals(""))
		_queryParameterValues.put("subject", subject);
	    if (predicate != null && !predicate.equals(""))
		_queryParameterValues.put("predicate", predicate);
	    if (object != null && !object.equals(""))
		_queryParameterValues.put("object", object);
	    if (isliteral != null)
		_queryParameterValues.put("isLiteral", isliteral);
	    if (datatype != null && !datatype.equals(""))
		_queryParameterValues.put("datatype", datatype);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doPOST(new ByteArrayDataSource("", DEFAULT_CONTENT_TYPE), DEFAULT_CONTENT_TYPE, _url, _headerParameterValues, null);
	    return ;
	}

	public void post(String subject, String predicate, String object, Boolean isliteral, String datatype)
	throws IOException, MalformedURLException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();

	    if (subject != null && !subject.equals(""))
		_queryParameterValues.put("subject", subject);
	    if (predicate != null && !predicate.equals(""))
		_queryParameterValues.put("predicate", predicate);
	    if (object != null && !object.equals(""))
		_queryParameterValues.put("object", object);
	    if (isliteral != null)
		_queryParameterValues.put("isLiteral", isliteral);
	    if (datatype != null && !datatype.equals(""))
		_queryParameterValues.put("datatype", datatype);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    retVal = _dsDispatcher.doPOST(new ByteArrayDataSource("", DEFAULT_CONTENT_TYPE), DEFAULT_CONTENT_TYPE, _url, _headerParameterValues, null);
	    return ;
	}

	/*public void postAsvoid(String subject, String predicate, String object, Boolean isliteral, String datatype)
	throws IOException, MalformedURLException, JAXBException
	{
	    HashMap<String, Object> _queryParameterValues = new HashMap<String, Object>();
	    HashMap<String, Object> _headerParameterValues = new HashMap<String, Object>();
	    _queryParameterValues.put("subject", subject);
	    _queryParameterValues.put("predicate", predicate);
	    _queryParameterValues.put("object", object);
	    _queryParameterValues.put("isLiteral", isliteral);
	    _queryParameterValues.put("datatype", datatype);
	    String _url = _uriBuilder.buildUri(_templateAndMatrixParameterValues, _queryParameterValues);
	    Object _retVal = _jaxbDispatcher.doPOST(null, null, _url, _headerParameterValues, null);
	    if (_retVal == null) {
		return ;
	    }
	    if (JAXBElement.class.isInstance(_retVal)) {
		JAXBElement<?> jaxbElement = ((JAXBElement<?>) _retVal);
		_retVal = jaxbElement.getValue();
	    }
	    return ;
	}
	 */
    }

}
